#coding:utf-8
import json
import sqlite3


# create sqlite db
cx = sqlite3.connect('tools.db')
print("db tools.db has created!")

# 打开游标
cu = cx.cursor()

# 创建cardBIN表
cu.execute(
    'CREATE TABLE IF NOT EXISTS cardBIN(bank_code VARCHAR(15), bank_name VARCHAR(30), card_BIN VARCHAR(15), card_length INT, card_type VARCHAR(30))')
print("table cardBIN has created!")

# 向cardBIN表中插入数据
with open('cardBIN.json') as of:
    for each in json.loads(of.read()):
        print('start load data of ' + each['card_BIN'])
        cu.execute('INSERT INTO cardBIN VALUES(?,?,?,?,?)',
                   (each['bank_code'], each['bank_name'], each['card_BIN'], each['card_length'], each['card_type']))
        cx.commit()
        print("cardBIN %s has loaded!" % (each['card_BIN']))
print("data has loaded!")
cu.close()
